﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Reflection;
using System.Text;

/// <summary>
/// 请求地址的附属参数操作
/// </summary>
public class RequestQuery
{
    /// <summary>
    /// 参数属性
    /// </summary>
    public class QueryAttribute : Attribute
    {
        string _QueryKey = string.Empty;
        /// <summary>
        /// 获取或设置参数 Key
        /// </summary>
        public string QueryKey
        {
            set { _QueryKey = value; }
            get { return _QueryKey; }
        }
    }

    /// <summary>
    /// 获取该对象的自定义属性
    /// </summary>
    /// <param name="FieldObject">字段成员</param>
    static QueryAttribute GetQueryAttribute(FieldInfo FieldObject)
    {
        object[] attrs = FieldObject.GetCustomAttributes(typeof(QueryAttribute), true);
        if (attrs != null && attrs.Length != 0) { return (QueryAttribute)attrs[0]; }
        return null;
    }

    /// <summary>
    /// 获取所有参数的值
    /// </summary>
    public static void SetQueryValues()
    {
        System.Web.UI.Page cpage = (System.Web.UI.Page)HttpContext.Current.CurrentHandler;
        object getval = null;
        QueryAttribute qa = null;

        //循环对象的公共成员
        foreach (FieldInfo fi in cpage.GetType().GetFields())
        {
            qa = GetQueryAttribute(fi);
            if (fi != null && qa != null)
            {
                getval = fi.GetValue(cpage);//参数默认值                
                Type fType = fi.FieldType;

                if (fType == typeof(string))
                {
                    string str_para = Convert.ToString(getval);
                    UIMethod.GetQuery(ref str_para, qa.QueryKey, true);
                    getval = str_para;
                }
                else if(fType == typeof(int))
                {
                    int int_para = Convert.ToInt32(getval);
                    UIMethod.GetQuery(ref int_para, qa.QueryKey);
                    getval = int_para;
                }
                else if(fType == typeof(DateTime))
                {
                    DateTime dt_para = Convert.ToDateTime(getval);
                    UIMethod.GetQuery(ref dt_para, qa.QueryKey);
                    getval = dt_para;
                }
                else if(fType == typeof(bool))
                {
                    bool bool_para = Convert.ToBoolean(getval);
                    UIMethod.GetQuery(ref bool_para, qa.QueryKey);
                    getval = bool_para;
                }
                else if(fType == typeof(float))
                {
                    float sig_para = Convert.ToSingle(getval);
                    UIMethod.GetQuery(ref sig_para, qa.QueryKey);
                    getval = sig_para;
                }
                else if(fType == typeof(double))
                {
                    double dou_para = Convert.ToDouble(getval);
                    UIMethod.GetQuery(ref dou_para, qa.QueryKey);
                    getval = dou_para;
                }
                else if(fType == typeof(long))
                {
                    long lon_para = Convert.ToInt64(getval);
                    UIMethod.GetQuery(ref lon_para, qa.QueryKey);
                    getval = lon_para;
                }
                else if(fType == typeof(string[]))
                {
                    string[] arr_para = (string[])getval;
                    UIMethod.GetQuery(ref arr_para, qa.QueryKey);
                    getval = arr_para;
                }

                try
                {
                    fi.SetValue(cpage, getval);
                }
                catch{}
            }
        }
    }

    /// <summary>
    /// 获取当前参数值
    /// </summary>    
    public static string GetQueryValues()
    {
        StringBuilder sb = new StringBuilder();
        System.Web.UI.Page cpage = (System.Web.UI.Page)HttpContext.Current.CurrentHandler;
        string para_tmp = "{0}={1}&";
        string para_val = string.Empty;
        object getval = null;
        QueryAttribute qa = null;

        //循环对象的公共成员
        foreach (FieldInfo fi in cpage.GetType().GetFields())
        {
            qa = GetQueryAttribute(fi);
            if (fi != null && qa != null)
            {
                getval = fi.GetValue(cpage);//参数默认值                
                Type fType = fi.FieldType;

                if (fType == typeof(string))
                {
                    para_val = cpage.Server.UrlEncode(Convert.ToString(getval));
                }
                else if (fType == typeof(DateTime))
                {
                    DateTime dt_val = Convert.ToDateTime(getval);
                    para_val = (dt_val == DateTime.MinValue) ? string.Empty : dt_val.ToShortDateString();
                }
                else if (fType == typeof(bool))
                {
                    para_val = Convert.ToInt32(getval).ToString();
                }
                else if (fType == typeof(string[]))
                {
                    string[] arr_para = (string[])getval;
                    para_val = UIMethod.UniteArray(arr_para, ",");
                }
                else
                {
                    para_val = Convert.ToString(getval);
                }

                sb.Append(string.Format(para_tmp, qa.QueryKey, para_val));
            }
        }

        return sb.ToString();
    }

    /// <summary>
    /// 根据实体类获取表单中的值
    /// </summary>
    /// <param name="TableEntity">代理类</param>
    /// <param name="WebForm">表单对象</param>
    public static void SetEntityValues(DataEntity.IEntity TableEntity, Control WebForm)
    {
        //获取实体类的抽象层
        Type T = TableEntity.GetType();
        Object obj = Assembly.GetAssembly(T).CreateInstance(T.FullName);
        //获取当前页上下文表单里面所有服务器控件
        //ControlCollection cc = ((System.Web.UI.Page)HttpContext.Current.CurrentHandler).Controls;
        //循环实体类成员
        Control con = null;
        object val = null;
        Type elementType = typeof(string);
        TypeCode tc = TypeCode.String;
        Type Tcon = null;

        foreach(FieldInfo fi in T.GetFields())
        {
            elementType = fi.GetValue(obj).GetType();//获取包含元素类型
            tc = Type.GetTypeCode(elementType);//获取成员类型常量
            
            try{
                con = WebForm.FindControl(fi.Name);
                if(con != null)
                {
                    //控件类型
                    Tcon = con.GetType();
                    if(Tcon == typeof(System.Web.UI.WebControls.TextBox))//文本框
                    {
                        fi.SetValue(TableEntity, Convert.ChangeType(((System.Web.UI.WebControls.TextBox)con).Text.Trim(), elementType));
                    }
                    else if(Tcon == typeof(System.Web.UI.WebControls.DropDownList))//下拉列表
                    {                        
                        val = SetMatchValue(tc, ((System.Web.UI.WebControls.DropDownList)con).SelectedValue);
                        fi.SetValue(TableEntity, Convert.ChangeType(val, elementType));
                    }
                    else if(Tcon == typeof(System.Web.UI.WebControls.RadioButtonList))//单选列表
                    {
                        val = SetMatchValue(tc, ((System.Web.UI.WebControls.RadioButtonList)con).SelectedValue);
                        fi.SetValue(TableEntity, Convert.ChangeType(val, elementType));                        
                    }
                    else if(Tcon == typeof(System.Web.UI.WebControls.CheckBox))//复选框
                    {
                        fi.SetValue(TableEntity, Convert.ChangeType(((System.Web.UI.WebControls.CheckBox)con).Checked, elementType));
                    }
                    else if (Tcon == typeof(System.Web.UI.WebControls.HiddenField))//隐藏框
                    {
                        fi.SetValue(TableEntity, Convert.ChangeType(((System.Web.UI.WebControls.HiddenField)con).Value, elementType));
                    }
                    else
                    {
                        //调用类反射的Text属性
                        PropertyInfo pi = con.GetType().GetProperty("Text");
                        if (pi != null)
                        {
                            fi.SetValue(TableEntity, Convert.ChangeType(pi.GetValue(con, null), elementType));
                        }
                        else
                        {
                            pi = con.GetType().GetProperty("Value");
                            if (pi != null)
                            {
                                fi.SetValue(TableEntity, Convert.ChangeType(pi.GetValue(con, null), elementType));
                            }
                        }
                    }
                }
            }catch{}
        }
    }

    /// <summary>
    /// 设置正确转换的数据类型
    /// </summary>
    /// <param name="sourceType">原型类型</param>
    /// <param name="val">被转换的对象</param>
    /// <returns></returns>
    public static object SetMatchValue(TypeCode sourceType, string val)
    {
        object rs = null;
        switch (sourceType)
        {
            case TypeCode.Boolean: rs = Convert.ToBoolean(Convert.ToInt32(val)); break;//布尔
            case TypeCode.Int32: rs = Convert.ToInt32(val); break;//数值
        }
        return rs;
    }

    /// <summary>
    /// 获取正确转换的数据类型
    /// </summary>
    /// <param name="sourceType">原型类型</param>
    /// <param name="val">被转换的对象</param>
    /// <returns></returns>
    public static string GetMatchValue(TypeCode sourceType, object val)
    {
        string rs = string.Empty;
        switch (sourceType)
        {
            case TypeCode.Boolean: rs = Convert.ToString(Convert.ToInt32(val)); break;//布尔
            case TypeCode.Int32: rs = Convert.ToString(val); break;//数值
        }
        return rs;
    }

    /// <summary>
    /// 根据实体类绑定表单中的值
    /// </summary>
    /// <param name="TableEntity">代理类</param>
    public static void GetEntityValues(DataEntity.IEntity TableEntity, Control WebForm)
    {
        //获取实体类的抽象层
        Type T = TableEntity.GetType();        
        //循环实体类成员
        Control con = null;
        object val = null;

        foreach (FieldInfo fi in T.GetFields())
        {
            try
            {
                con = WebForm.FindControl(fi.Name);
                if (con != null)
                {
                    Type Tcon = con.GetType();
                    if (Tcon == typeof(System.Web.UI.WebControls.TextBox))//文本框
                    {
                        ((System.Web.UI.WebControls.TextBox)con).Text = Convert.ToString(fi.GetValue(TableEntity));
                    }
                    else if (Tcon == typeof(System.Web.UI.WebControls.DropDownList))//下拉列表
                    {
                        val = fi.GetValue(TableEntity);
                        ((System.Web.UI.WebControls.DropDownList)con).SelectedValue = GetMatchValue(Type.GetTypeCode(val.GetType()), val);
                    }
                    else if (Tcon == typeof(System.Web.UI.WebControls.RadioButtonList))//单选列表
                    {
                        val = fi.GetValue(TableEntity);
                        ((System.Web.UI.WebControls.RadioButtonList)con).SelectedValue = GetMatchValue(Type.GetTypeCode(val.GetType()), val);
                    }
                    else if (Tcon == typeof(System.Web.UI.WebControls.CheckBox))//复选框
                    {
                        ((System.Web.UI.WebControls.CheckBox)con).Checked = Convert.ToBoolean(fi.GetValue(TableEntity));
                    }
                    else if (Tcon == typeof(System.Web.UI.WebControls.HiddenField))//隐藏框
                    {
                        ((System.Web.UI.WebControls.HiddenField)con).Value = Convert.ToString(fi.GetValue(TableEntity));
                    }
                    else if (Tcon == typeof(System.Web.UI.WebControls.Literal))//占位符
                    {
                        ((System.Web.UI.WebControls.Literal)con).Text = Convert.ToString(fi.GetValue(TableEntity));
                    }
                    else if (Tcon == typeof(System.Web.UI.WebControls.Label))//占位符
                    {
                        ((System.Web.UI.WebControls.Label)con).Text = Convert.ToString(fi.GetValue(TableEntity));
                    }
                    else 
                    {
                        //调用类反射的Text属性
                        PropertyInfo pi = con.GetType().GetProperty("Text");
                        if (pi != null)
                        {
                            pi.SetValue(con, Convert.ToString(fi.GetValue(TableEntity)), null);
                        }
                        else
                        {
                            pi = con.GetType().GetProperty("Value");
                            if (pi != null)
                            {
                                pi.SetValue(con, Convert.ToString(fi.GetValue(TableEntity)), null);
                            }
                        }
                    }
                }
            }
            catch { }
        }
    }
}
